using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Common;
using IGlobalist.Cms.Bll;
using IGlobalist.Cms.Model;

using System.Collections.Generic;

namespace IGlobalist.Cms.Admin.Control
{
    public partial class RoleAddEdit : AdminBaseUserControl
    {
        public event EventHandler EventReloadList;
        public event EventHandler SaveRole;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// bind detail by  Selected ClassList
        /// </summary>
        /// <param name="?"></param>
        public void DoBindRoleDetail(object sender, EventArgs e)
        {
            fsExt.Visible = false;
            RoleList roleList = (RoleList)sender;
            lblStatus.Text = "Edit Role";
            long ID = Convert.ToInt64(roleList.SelectedObjectValue);
            IRole role = RoleController.GetRole(ID);
            ddlClass.SelectedValue = role.ClassID.ToString();
            ViewState["Role"] = role;
            ddlRoleName.SelectedValue = role.Name;
            ddlClass.DataBind();
            //ddlClass.SelectedIndex = 0;
            ICmsClass clazz = ClassController.GetClass(Convert.ToInt64(ddlClass.SelectedValue));
            if (clazz != null)
            {
                ucCmsExtPropertiesForm.PropertyList = clazz.PropertyList;
            }
            if (ucCmsExtPropertiesForm.PropertyList.Count > 2)
                fsExt.Visible = true;
            else fsExt.Visible = false;
            ddlRoleName.Enabled = false;
            tbDescription.Text = role.Description;
        }

        private IRole GetRole()
        {
            IRole role = new Role();
            if (ViewState["Role"] != null)
            {
                role = (IRole)ViewState["Role"];
            }
            IList<IRole> roleList = RoleController.GetRoleList(Convert.ToInt64(ddlClass.SelectedValue));
            Session[SessionVariable.Clazz.CurrentID] = Convert.ToInt64(ddlClass.SelectedValue);
            lblError.Text = "";
            this.IsDuplicateRoleName = false;
            foreach (IRole r in roleList)
            {
                if (r.Name == ddlRoleName.SelectedValue && ViewState["Role"] == null)
                {
                    this.IsDuplicateRoleName = true;
                    lblError.Text = "*";
                    return null;
                }
            }
            role.Class = ClassController.GetClass(Convert.ToInt64(ddlClass.SelectedValue));
            role.ClassID = Convert.ToInt64(ddlClass.SelectedValue);
            role.ClassTypeID = Convert.ToInt16(CmsObjectType.Role);
            if (String.IsNullOrEmpty(tbDescription.Text) && String.IsNullOrEmpty(ddlRoleName.SelectedValue))
            {
                return null;
            }
            else
            {
                role.Description = tbDescription.Text;
                role.Name = ddlRoleName.SelectedValue;
                role.ExtPropertyList = ucCmsExtPropertiesForm.PropertyList;
            }

            return role;

        }
        public void DoInsertRole(object sender, EventArgs e)
        {
            string status = "Create new Role";
            bool success = false;
            IRole role = GetRole();
            if (role != null)
            {
                if (role.ID > 0)
                {
                    role.EditedBy = IGlobalistMembership.GetUser().ID;
                    RoleController.UpdateRole(role);
                    status = "Update Successful";
                    success = true;
                }
                else
                {
                    RoleController.InsertRole(role);
                    status = "Insert Successful";
                    success = true;
                }
            }

            if (role != null && success == true)
            {
                // EventReloadList(this, e);
                SaveRole(this, e);

                ddlClass.Enabled = false;
            }

            lblErrorSite.Text = "";
            if (this.IsDuplicateRoleName)
            {
                lblErrorSite.Text = "Role name is Exist";
            }


            lblStatus.Text = status;
        }
        public void DoHidden(object sender, EventArgs e)
        {
            this.Visible = false;
        }
        public void DoShow(object sender, EventArgs e)
        {
            this.Visible = true;
        }
        public void DoDeleteRole(object sender, EventArgs e)
        {
            if (ViewState["Role"] != null)
            {
                IRole role = (IRole)ViewState["Role"];
                long ID = role.ID;
                RoleController.DeleteRole(ID);
            }
        }

        public void DoBackRole(object sender, EventArgs e)
        {

        }

        public void DoNew(object sender, EventArgs e)
        {
            tbDescription.Focus();
            ViewState["Role"] = null;
            this.IsDuplicateRoleName = false;
            lblErrorSite.Text = "";
            tbDescription.Text = "";
            ddlClass.DataBind();
            ddlClass.Enabled = true;
            lblError.Text = "";
            //ddlClass.SelectedIndex = 0;
            fsExt.Visible = false;
            ddlRoleName.Enabled = true;
            ddlRoleName.SelectedIndex = -1;
            lblStatus.Text = "Add New Role";
            ICmsClass clazz = ClassController.GetClass(Convert.ToInt64(ddlClass.SelectedValue));
            if (clazz != null)
            {
                ucCmsExtPropertiesForm.PropertyList = clazz.PropertyList;
            }
            if (ucCmsExtPropertiesForm.PropertyList.Count > 2)
                fsExt.Visible = true;

        }

        public void DoDuplicate(object sender, EventArgs e)
        {
            if (ViewState["Role"] != null)
            {
                IRole role = (IRole)(ViewState["RoleID"]);
                if (role != null)
                {
                    //RoleController.InsertRole(role);
                    ViewState["Role"] = null;
                    tbDescription.Text = role.Description;
                    foreach (ListItem item in ddlRoleName.Items)
                    {
                        item.Selected = false;
                    }
                    ddlRoleName.Items.FindByValue(role.Name).Selected = true;
                    ICmsClass clazz = ClassController.GetClass(Convert.ToInt64(ddlClass.SelectedValue));
                    if (clazz != null)
                    {
                        ucCmsExtPropertiesForm.PropertyList = clazz.PropertyList;
                    }
                }
            }
        }

        protected void ddlClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session[SessionVariable.Clazz.CurrentID] = ddlClass.SelectedValue;
            ICmsClass clazz = ClassController.GetClass(Convert.ToInt64(ddlClass.SelectedValue));
            ucCmsExtPropertiesForm.PropertyList = clazz.PropertyList;
            if (ucCmsExtPropertiesForm.PropertyList.Count > 2) fsExt.Visible = true;
            else fsExt.Visible = false;
        }
        #region Property
        public bool IsDuplicateRoleName
        {
            get
            {
                if (ViewState["IsDuplicateRoleName"] == null) return false;
                else return (bool)ViewState["IsDuplicateRoleName"];
            }
            set
            {
                ViewState["IsDuplicateRoleName"] = value;
            }
        }
        #endregion
    }
}
